//
//  SEExamineRecordEyePressureViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/26.
//

import UIKit

class SEExamineRecordEyePressureViewModel: SEExamineRecordBaseViewModel {

    override var resultDescription: String {
        var str = ""
        str += "眼压测量结果(平均数值)--"

        if let models = eyePressureCellModels.last, models.count > 0 {
            if showRight {
                str += "右眼眼压: \(models[0].valueText);"
            }
            if showLeft {
                str += "左眼眼压: \(models[models.count - 1].valueText);"
            }
        }
        return str
    }

    override var numberOfSections: Int {
        return model.dataDTO.count > 0 ? model.dataDTO.count + 1 : 0
    }

    override func numberOfRowInSection(section: Int) -> Int {
        if section == numberOfSections - 1 {
            return model.imageUrls.count
        }
        return eyePressureCellModels[section].count
    }

    override func heightForRow(indexPath: IndexPath) -> CGFloat {
        return indexPath.section == (numberOfSections - 1) ? 160 : 56
    }

    override func titleForHeaderInSection(section: Int) -> String {
        if section == numberOfSections - 1 {
            return model.imageUrls.count > 0 ? "图像" : ""
        } else if section == numberOfSections - 2 {
            return "测量平均值"
        } else {
             return "第 \(section + 1) 次测量"
        }
    }

    func eyePressureCellModel(indexPath: IndexPath) -> SEExamineRecordEyePressureCellModel {
        return eyePressureCellModels[indexPath.section][indexPath.row]
    }

    var eyePressureCellModels: [[SEExamineRecordEyePressureCellModel]] {
        var modelArrays = [[SEExamineRecordEyePressureCellModel]]()
        model.dataDTO.forEach {
            var modelArray = [SEExamineRecordEyePressureCellModel]()
            for i in 0..<$0.count {
                if i == 0 && showRight {
                    let model = SEExamineRecordEyePressureCellModel(titleText: "右眼", valueText: $0[i].eyeData)
                    modelArray.append(model)
                }
                if i == 1 && showLeft {
                    let model = SEExamineRecordEyePressureCellModel(titleText: "左眼", valueText: $0[i].eyeData)
                    modelArray.append(model)
                }
            }
            modelArrays.append(modelArray)
        }
        return modelArrays
    }
    
}
